// 引入axios
import axios from "axios";
// import qs from "qs"
// import { showToast, showLoadingToast, showFailToast, closeToast } from 'vant';
// import { doEncrypt, doDecryptStr } from './crypto'

// let toast = null;

// 创建时自定义默认配置，超时设置为全局默认值0秒
let service = axios.create({
  // url = base url + request url
  baseURL: import.meta.env.VITE_APP_BASE_URL,
  // withCredentials: true,
  // send cookies when cross-domain requests
  // request timeout
  timeout: 50000,
  headers: {
    "Access-Control-Allow-Origin": "*",
    "Content-Type": "application/x-www-form-urlencoded",
    //   'Content-Type': 'application/json'
  },
});
// 请拦截
service.interceptors.request.use(
  (config) => {
    // loading
    // toast = showLoadingToast({
    //   forbidClick: true,
    //   loadingType: 'spinner',
    //   message: '加载中',
    // });

    let token = localStorage.getItem("token");
    if (token) {
      config.headers["token"] = token;
    }
    //    console.log(config.data,'加密前参数')
    // config.data = qs.stringify({ json: doEncrypt(config.data) });
    //    console.log(config.data,'加密后参数')
    return config;
  },
  (error) => {
    // do something with request error
    // 清除loading
    // closeToast()
    console.log(error); // for debug
    return Promise.reject(error);
  }
);

// 响应拦截
service.interceptors.response.use(
  (response) => {
    // 清除loading
    // closeToast()
    //   console.log('response',response);
    let { data } = response;
    // if (data.Status !== 1)
    //   return showFailToast(data.Msg);
    // return doDecryptStr(data.Data)
    return data;
  },
  (error) => {
    // for debug
    console.log("err" + error);
    // showFailToast(error || "请求失败");
    // return Promise.reject(error)
  }
);

export default service;
